package fr.smile.sandbox.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import fr.smile.sandbox.Product;
import fr.smile.sandbox.ProductDAO;

public class ProductDeleteController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		ProductDAO myProduct = (ProductDAO) context.getBean("productDAO");
		long id = Long.parseLong(request.getParameter("id"));
		Product product = myProduct.getProductByID(id);
		myProduct.deleteProduct(product);
		return new ModelAndView("redirect:list.html");
	}

}
